/**
 * @(#)com.framework.demo.controller.UserController
 *
 * Copyright (c) 2014-2018 上海纬视瑞科技有限公司
 *
 * DESC:
 *
 */
package com.framework.demo.controller;

import com.framework.demo.bean.User;
import com.framework.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author fusf
 * @version 1.0  2017/4/14
 */
@Controller
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping(value = "/user/getUserList", method = RequestMethod.GET)
    @ResponseBody
    public List<User> getUserList() {
        return userService.findAll();
    }

    @RequestMapping(value = "/user/add", method = RequestMethod.GET)
    @ResponseBody
   /* @SystemControllerValidate*/
    public Map<String, Object> addUser(@Validated User user, BindingResult result) {
        Map map = new HashMap();
        if (result.hasErrors()) {
            map.put("result", false);
            map.put("message", result.getFieldError().getDefaultMessage());
            return map;
        }
        return null;
    }

    @RequestMapping(value = "/user/update", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> update() {
        Map map = new HashMap();
        User user = new User();
        user.setId(1L);
        user.setName("333");
        userService.updateUser(user);
        map.put("result", true);
        map.put("message", "成功");
        return map;
    }

     @RequestMapping(value = "/user/insert", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> insert() {
        Map map = new HashMap();
        User user = new User();
        user.setName("333");
        userService.insertUser(user);
        map.put("result", true);
        map.put("message", "成功");
        return map;
    }


}
